import json
import os
import csv

from pyassured.core.project_path import config
from pyassured.core.logger import Log
from pyassured.core.function_helper import FunctionHelper

from itertools import islice

log = Log()


class TestData:

    @staticmethod
    def get_json_data(filename):

        filename = (os.path.join(config.data_dir, filename))

        with open(filename, 'r', encoding="utf-8") as f:
            data = json.load(f)

            data_arr = data.get("data")
            data_tuple_arr = []
            for d in data_arr:
                data_arr_temp = []
                for value in d.values():
                    data_arr_temp.append(FunctionHelper.parse_function_str(value))
                data_tuple_arr.append(tuple(data_arr_temp))

            log.info("读取JSON参数化数据,文件名:{},数据:{}".format(filename, data_tuple_arr))
            return data_tuple_arr

    @staticmethod
    def get_csv_data(filename, ignore=True):

        filename = (os.path.join(config.data_dir, filename))
        data_tuple_arr = []

        line_num = 1

        if not ignore:
            line_num = 0

        with open(filename, 'r', encoding="utf-8") as file:

            csv_file = csv.reader(islice(file, line_num, None))
            for line in csv_file:

                temp_arr = []
                for data in line:
                    temp_arr.append(FunctionHelper.parse_function_str(data))

                data_tuple_arr.append(tuple(temp_arr))

        log.info("读取CSV参数化数据,文件名:{},数据:{}".format(filename, data_tuple_arr))
        return data_tuple_arr
